/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.cjb.malacma.rssfeed.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.cjb.malacma.rssfeed.persistence.JPAController;

/**
 *
 * @author malacma
 */
public class PinoController extends HttpServlet {

    private static final String NM_CLASS = "class";
    private static final String NM_METHOD = "method";
    private static final String DEFAULT_CLASS = "net.cjb.malacma.rssfeed.util.FeedFacade";
    private static final String DEFAULT_METHOD = "processFeedInfo";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String nmClass, nmMethod;
            nmClass = request.getParameter(NM_CLASS) == null ? DEFAULT_CLASS : request.getParameter(NM_CLASS);
            nmMethod = request.getParameter(NM_METHOD) == null ? DEFAULT_METHOD : request.getParameter(NM_METHOD);
            ActionInvoke.execute(nmClass, nmMethod, request, response);
            nmClass = null;
            nmMethod = null;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PinoController.class.getName()).log(Level.WARNING, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(PinoController.class.getName()).log(Level.WARNING, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PinoController.class.getName()).log(Level.WARNING, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(PinoController.class.getName()).log(Level.WARNING, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(PinoController.class.getName()).log(Level.WARNING, null, ex);
        }catch (Exception ex) {
            Logger.getLogger(PinoController.class.getName()).log(Level.FINEST, null, ex);
        } finally {
            out.close();
            System.gc();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "PINO - http://malacma.cjb.net";
    }// </editor-fold>
}
